package com.bw.miaosha.controller;

import java.util.*;

import com.bw.miaosha.conf.MyInterceptor;
import com.bw.miaosha.dao.GoodsDao;
import com.bw.miaosha.dao.OrderInfoDao;
import com.bw.miaosha.entity.GoodsEntity;
import com.bw.miaosha.entity.OrderInfoEntity;
import com.bw.miaosha.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bw.miaosha.entity.MiaoshaActivityEntity;
import com.bw.miaosha.service.MiaoshaActivityService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 
 *
 * @author zhucaiyi
 * @email zhucaiyi@gmail.com
 * @date 2022-02-22 11:27:06
 */
@RestController
@RequestMapping("miaosha/miaoshaactivity")
@FeignClient
public class MiaoshaActivityController {
    @Autowired
    private MiaoshaActivityService miaoshaActivityService;

    @Autowired
    private GoodsDao goodsDao;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = miaoshaActivityService.queryPage(params);


        return R.ok().put("page", page);
    }



    @Autowired
    private MyInterceptor myInterceptor;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){


        String s = UUID.randomUUID().toString();
        String token = s.replace("-", "");
        MiaoshaActivityEntity miaoshaActivity = miaoshaActivityService.getById(id);
        miaoshaActivity.setToken(token);
        String userkey = redisTemplate.opsForValue().get("userkey");
        String[] split = userkey.split(",");
        redisTemplate.opsForValue().set(split[0], token);
        return R.ok().put("miaoshaActivity", miaoshaActivity);
    }

    @Autowired
    private OrderInfoDao orderInfoDao;


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MiaoshaActivityEntity miaoshaActivity){

        String token = miaoshaActivity.getToken();
        String userkey = redisTemplate.opsForValue().get("userkey");
        String[] split = userkey.split(",");
        String redistoken = redisTemplate.opsForValue().get(split[0]);
        OrderInfoEntity orderInfoEntity = new OrderInfoEntity();


            System.out.println("生成订单");

            orderInfoEntity.setCreateDate(new Date());
            orderInfoEntity.setDeliveryAddrId(0L);
            orderInfoEntity.setGoodsChannel(1);
            orderInfoEntity.setGoodsCount(1);
            orderInfoEntity.setGoodsCount(orderInfoEntity.getGoodsCount());
            String replace = UUID.randomUUID().toString().replace("-", "");
            orderInfoEntity.setId(replace);
            orderInfoEntity.setStatus(0);
            orderInfoEntity.setGoodsId(miaoshaActivity.getGoodsId());
            orderInfoEntity.setUserId(Long.parseLong(split[0]));
            orderInfoEntity.setGoodsPrice(miaoshaActivity.getMiaoshaPrice());

        GoodsEntity goodsEntity = goodsDao.selectById(miaoshaActivity.getGoodsId());
        orderInfoEntity.setGoodsName(goodsEntity.getGoodsName());
            orderInfoDao.insert(orderInfoEntity);
            return R.ok().put("oid",orderInfoEntity.getId());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MiaoshaActivityEntity miaoshaActivity){
		miaoshaActivityService.updateById(miaoshaActivity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		miaoshaActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
